<?php

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\console\input\Argument;
use think\Db;
use Im\Yunxinapi;

class User extends Command
{
    protected function configure()
    {
        $this->setName('User')
            ->addArgument('user_type', Argument::OPTIONAL)
            ->addArgument('number', Argument::OPTIONAL)
            ->addArgument('email', Argument::OPTIONAL)
           // ->addArgument('longLat', Argument::OPTIONAL)
            ->addArgument('lat', Argument::OPTIONAL)
            ->addArgument('lng', Argument::OPTIONAL)
            ->addArgument('user_nickname', Argument::OPTIONAL)
            ->setDescription('批量生成用户 ');
    }

    //执行批量生成用户的脚本
    protected function execute(Input $input, Output $output)
    {
        $user_type = trim($input->getArgument('user_type'));
        $number = trim($input->getArgument('number'));
        $email_ = trim($input->getArgument('email'));
       // $longLat = trim($input->getArgument('longLat'));
        $lat = trim($input->getArgument('lat'));
        $lng = trim($input->getArgument('lng'));
        $user_nickname = trim($input->getArgument('user_nickname'));
        $user_type = $user_type ?: 1;
        $number = $number ?: 10;
        $email_ = $email_ ?: 'cnsellers_';
        // 1、服务商 2、供应商 3、采购商
        $user_role = ($user_type == 1) ? 2 : (($user_type == 2) ? 3 : (($user_type == 3) ? 1 : 0));
        for ($i = 0; $i < $number; $i++) {
            //自定义一些数据
            $user_id = time();
            //********************循环开始
            $type = 2;         // '1:中国区用户  2:非中国区用户';
            $password = md5('123456');     // '密码';
            $user_logo = $this->getRandAvatars(); //随机头像
            $email = $email_ . $user_id . '@163.com';
            $mobile = '';
            $countrycode = 86;
            //$user_nickname = getUserNikeName(); //用户随机昵称
            //$user_nickname = getCompanyName($longLat); //用户随机昵称
            $user_ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '127.0.0.1';
            $environment_prefix_user_name = $user_id;

            //开启事务
            DB::startTrans();
            try {
                //ucenter库 --- start
                $ucenter_user_bind_connect_data_email = array(
                    'bind_id' => 'email_' . $email,
                    'bind_type' => 11,
                    'user_id' => $user_id,
                    'bind_time' => date("Y-m-d H:i", $user_id)
                );
                DB::table('ucenter_user_bind_connect')->insert($ucenter_user_bind_connect_data_email);

                $ucenter_user_info_detail_data = array(
                    'user_name' => config('environment_prefix_user_name') . $user_id,
                    'user_mobile_county_code' => $countrycode,
                    'user_mobile' => $mobile,
                    'user_email' => $email,
                    'user_mobile_verify' => 0,
                    'user_email_verify' => 1
                );
                DB::table('ucenter_user_info_detail')->insert($ucenter_user_info_detail_data);

                $ucenter_user_info_data = array(
                    'user_id' => $user_id,
                    'user_name' => config('environment_prefix_user_name') . $user_id,
                    'password' => $password,
                    'action_time' => $user_id,
                    'action_ip' => $user_ip,
                    'session_id' => uniqid(),
                    'user_type' => 2,
                    'is_test' => 1,
                    'user_country_type' => $type
                );
                Db::table('ucenter_user_info')->insert($ucenter_user_info_data);
                //ucenter库 --- end


                $lng_lat = getLngLat($longLat);//获取随机经纬度
                $rand_index = rand(0, 20);
                //shop库 --- start
                $cn_user_info_data = array(
                    'user_id' => $user_id,
                    'user_nickname' => $user_nickname ? $user_nickname : $environment_prefix_user_name,
                    'user_mobile' => $mobile,
                    'user_mobile_county_code' => $countrycode,
                    'user_email' => $email,
                    'user_name' => config('environment_prefix_user_name') . $user_id,
                    'user_regtime' => date("Y-m-d H:i", time()),
                    'user_role' => $user_role,
                    'user_logo' => $user_logo,
                    'user_last_lng' => $lat,//经度
                    'user_last_lat' => $lng,//维度
                );
                Db::table('cn_user_info')->insert($cn_user_info_data);

                $cn_user_base_data = array(
                    'user_id' => $user_id,
                    'user_account' => config('environment_prefix_user_name') . $user_id,
                    'user_key' => 'ttt',
                    'user_login_times' => 1,
                    'user_login_time' => date("Y-m-d H:i", time()),
                    'user_login_ip' => $user_ip,
                    'user_type' => $user_type,
                    'seller_type' => 0
                );
                Db::table('cn_user_base')->insert($cn_user_base_data);
                if ($user_type == 3) {
                    //shop库 --- end
                    $service_id = getUniqueId('s_');
                    Db::table('cn_service_base')->insert(['user_id' => $user_id, 'service_id' => $service_id]);
                    Db::table('cn_service_company')->insert(['service_id' => $service_id]);
                }

                Db::commit();
                //注册云信账号
                $Yx = new Yunxinapi();
                $result = $Yx->createUserId($cn_user_info_data['user_name'], $cn_user_info_data['user_nickname'],
                    '');
                DB::name('cn_user_info')->where('user_id', $user_id)
                    ->update(['im_user_token' => $result['info']['token']]);
                //*******************注册完成

            } catch (\Exception $e) {
                Db::rollback();
                throw $e;
            }


            sleep(1);//防止生成的id冲突,延迟1秒后循环
            //********************循环截止
        }

        $output->writeln("生成用户完成");
    }

    //获取随机头像
    public function getRandAvatars()
    {
        $curl = new  \tp_tools\Curl;
        $path_row = ROOT_PATH . 'public' . DS . 'avatars300' . DS . sprintf("%'04d", rand(1, 300)) . '.jpg';
        $upfile = ['upfile' => new \CURLFile($path_row, 'image/jpeg')];
        $remote_url = config('remote_url');
        $remote = $curl->post($remote_url, $upfile, true);
        if ($remote) {
            $remote = json_decode($remote, true);
            if ($remote['ststus'] == 200) {
                if (isset($remote['data']['url'])) {
                    return $remote['data']['url'];
                } else {
                    return '';
                }
            }
        } else {
            return '';
        }

    }


}